// code by 林涛

getUserList(1)


function getUserList(pageNum){
    $.ajax({
        type:"post",
        url:"/system/getUserList",
        data:{pageNum:pageNum},
        dataType:"json",
        success:function (data){
            console.log(data)
            if(data!=null){
                var html = "        <tr>\n" +
                    "          <th>序号</th>\n" +
                    "          <th>员工编号</th>\n" +
                    "          <th>员工姓名</th>\n" +
                    "          <th>联系电话</th>\n" +
                    "          <th>所属部门</th>\n" +
                    "          <th>职位</th>\n" +
                    "          <th>性别</th>\n" +
                    "          <th>年龄</th>\n" +
                    "          <th>状态</th>\n" +
                    "          <th>入职时间</th>\n" +
                    "          <th>离职时间</th>\n" +
                    "          <th>操作</th>\n" +
                    "        </tr>"
                for (let i = 0; i < data.data.length; i++) {
                    var sex = "";
                    if(data.data[i].sex==1){
                        sex = "男"
                    }else {
                        sex = "女"
                    }
                    var status = "";
                    if(data.data[i].statusId==1){
                        status = "在职"
                    }else {
                        status = "离职"
                    }
                    html += "<tr>\n" +
                        "          <td>"+(i+1)+"</td>\n" +
                        "          <td>"+data.data[i].uId+"</td>\n" +
                        "          <td>"+data.data[i].uname+"</td>\n" +
                        "          <td>"+data.data[i].utelephone+"</td>\n" +
                        "          <td>"+data.data[i].dept.dept_name+"</td>\n" +
                        "          <td>"+data.data[i].userJob.job_name+"</td>\n" +
                        "          <td>"+sex+"</td>\n" +
                        "          <td>"+getAge(data.data[i].birthday)+"</td>\n" +
                        "          <td>"+status+"</td>\n" +
                        "          <td>"+getFormtTime(data.data[i].hiredate,true)+"</td>\n" +
                        "          <td>"+getFormtTime(data.data[i].leavedate,true)+"</td>\n" +
                        "          <td>\n" +
                        "            <a href=\"userUpdate.html\" class=\"tablelink\">修改</a>\n"
                    if(status == "在职"){
                        html += "<a href=\"javascript:void(0);\" class=\"tablelink\" onclick=\"tipOpen('您确定此员工离职吗？')\">离职</a>\n"
                    }
                    html +=   "          </td>\n" +
                        "        </tr>"
                }
                $(".tablelist").html(html)
                $(".totalPage").html(data.totalRows)
                $(".pageNum").html(data.pageNum)
                totalRows = data.totalRows
            }
        },error:function (){
            alert("出错了")
        }
    })
}


//格式化时间
//dateTime：时间戳
function getFormtTime(dateTime,flag) {
    if(dateTime != null ) {
        //若传入的dateTime为字符串类型，需要进行转换成数值，若不是无需下面注释代码
        var time = parseInt(dateTime)
        var date = new Date(time);
        //获取年份
        var YY = date.getFullYear();
        //获取月份
        var MM = (date.getMonth() + 1 < 10 ? '0'+(date.getMonth() + 1) : date.getMonth() + 1);
        //获取日期
        var DD = (date.getDate() < 10 ? '0'+date.getDate() : date.getDate());
        if(flag) { //flag为true，显示时分秒格式
            //获取小时
            var hh = (date.getHours() < 10 ? '0'+date.getHours() : date.getHours());
            //获取分
            var mm = (date.getMinutes() < 10 ? '0'+date.getMinutes() : date.getMinutes());
            ///获取秒
            var ss = (date.getSeconds() < 10 ? '0'+date.getSeconds() : date.getSeconds());
            //返回时间格式： 2020-11-09 13:14:52
            return YY + '-' + MM + '-' + DD + ' ' + hh + ':' + mm + ':' + ss;
        } else {
            //返回时间格式： 2020-11-09
            return YY + '-' + MM + '-' + DD;
        }
    } else {
        return "";
    }
}

function getAge(dateTime){
    if(dateTime != null ) {
        //若传入的dateTime为字符串类型，需要进行转换成数值，若不是无需下面注释代码
        var time = parseInt(dateTime)
        var nowYear = new Date();
        var date = new Date(time);
        //获取年份
        var YY = date.getFullYear();
        var now = nowYear.getFullYear();
        return now - YY
    } else {
        return "";
    }
}
